/* Copyright (c) 2012 Mobile Developer Solutions. All rights reserved.
 * This software is available under the MIT License:
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
//$('#page-home').live('pageinit', function(event) {
//	
//};
var mapdata = {
	destination : new google.maps.LatLng(59.3327881, 18.064488100000062)
};

function fadingMsg(locMsg) {
	$(
			"<div class='ui-overlay-shadow ui-body-e ui-corner-all fading-msg'>"
					+ locMsg + "</div>").css({
		"display" : "block",
		"opacity" : 0.9,
		"top" : $(window).scrollTop() + 100
	}).appendTo($.mobile.pageContainer).delay(2200).fadeOut(1000, function() {
		$(this).remove();
	});
}

$('#page-home').live('pagebeforecreate', function(event) {
	$.mobile.changePage("#dialog-home", "slidedown", false, false);
	

	
	function alertDismissed() {
        // do something
    }

   
	
	
});




// Create the map then make 'displayDirections' request
$('#page-map')
		.live(
				"pageinit",
				function() {
					// Define a default location and create the map
					var defaultLoc = new google.maps.LatLng(32.802955,
							-96.769923);
					$('#map_canvas')
							.gmap(
									{
										'center' : defaultLoc,
										'zoom' : 14,
										'zoomControlOptions' : {
											'position' : google.maps.ControlPosition.LEFT_TOP
										}
									})
							.bind(
									'init',
									function(evt, map) {
										// Try to get current location to center
										// on, else stay at defaultLoc
					$('#map_canvas').gmap(
														'getCurrentPosition',
														function(pos, status) {
															if (status === "OK") {
																var latLng = new google.maps.LatLng(
																		pos.coords.latitude,
																		pos.coords.longitude);
																
			$('#map_canvas')	.gmap(	'option','center',	latLng);
																
//		 Marker POSIZIONE E BOX INFO CON click SUL  Marker CORRENTE COMUNQUE SECONDO ME 
			
//	CI STA UN PO DI CASINO TRA I FILE ANDREBBERO RIORDINATI E FATTO TUTTO CON PRECISIONE CREDO CHE 
//	ORA POSSIAMO INIZIARE L APP COME SI DEVE
//		IN OLTRE MI SEMBRA CHE PER GESTIRE LE MAPE BASTA MOLTO MENO CODICE CHE NE DICI DI RAFFINARLO
		$('#map_canvas').gmap('addMarker', {
			'position': new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude), 'animation': google.maps.Animation.DROP 
			} ).click(function() {
                $('#map_canvas').gmap('openInfoWindow', { 'content': 'Tu Sei Qui' }, this);
	        });                                                                                          
		
	
		
		
		
		
		
		
		
		

															} else {
																fadingMsg("<span style='color:#f33;'>Error</span> while getting location. Device GPS/location may be disabled.");
																$.mobile.changePage($('#page-home'), "flip", false, false);
															}
														},
														{
															timeout : 6000,
															enableHighAccuracy : true
														});
									});
					
					
					
				});
$('#page-map').live("pageshow", function() {
	// if (navigator.geolocation ) {
	// fadingMsg('Ricerca della posizione tramite il sistema di
	// geolocalizzazione
	// del dispositivo.');
	// navigator.geolocation.getCurrentPosition (
	// function(position) {
	// alert(position);
	// $('#map_canvas').gmap({
	// 'center' : new google.maps.LatLng(position.coords.latitude,
	// position.coords.longitude),
	// 'mapTypeControl' : true
	// }).bind('init', function() {
	// $('.refresh').trigger('tap');
	// });
	// },
	// function(){
	// alert('Impossibile ricercare la posizione');
	// $.mobile.changePage($('#page-home'), {transition: flip});
	// });
	// } else {
	// alert('Impossibile ricercare la posizione');
	// }
	// fadingMsg ("Caricamento mappa");
	$('#map_canvas').gmap('refresh');
});
